package com.lyf.scm.admin.api.controller;

import com.lyf.scm.admin.api.dto.GmvTargetDTO;
import com.lyf.scm.admin.domain.excel.data.GmvWriteData;
import com.lyf.scm.admin.domain.excel.template.GmvTemplateOne;
import com.lyf.scm.admin.domain.excel.template.GmvTemplateTwo;
import com.lyf.scm.admin.domain.service.GmvTargetService;
import com.lyf.scm.admin.domain.util.ExcelUtil;
import com.lyf.scm.common.enums.GmvImportTypeEnum;
import com.lyf.scm.common.enums.ResponseMsg;
import com.lyf.scm.common.model.ExcelExportDTO;
import com.lyf.scm.common.util.ExcelExportUtil;
import com.rome.arch.core.clientobject.Response;
import com.rome.arch.core.exception.RomeException;
import com.rome.arch.util.controller.RomeController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * description:
 * gmv指标Controller
 *
 * @author: haodong
 * @data: 2020/7/8 16:20
 */
@Slf4j
@Api(tags = "gmv目标")
@RequestMapping("/api/v1/gmvTarget")
@RomeController
public class GmvTargetController {

    @Resource
    private GmvTargetService gmvTargetService;

    @PostMapping("/list")
    @ApiOperation(value = "按条件查询gmv指标")
    public Response listBySelective(@RequestBody GmvTargetDTO gmvTarget) {
        return Response.builderSuccess(gmvTargetService.listBySelective(gmvTarget));
    }

    @PostMapping("/listAll")
    @ApiOperation(value = "查询全部gmv指标")
    public Response listAllBySelective(@RequestBody GmvTargetDTO gmvTarget) {
        return Response.builderSuccess(gmvTargetService.listAllBySelective(gmvTarget));
    }

    @PostMapping("/importVerification")
    @ApiOperation("导入验证")
    public Response importVerification(@RequestBody MultipartFile file,@RequestParam Integer type) {
        return Response.builderSuccess(gmvTargetService.importVerification(file,type));
    }

    @PostMapping("/exportExcel")
    @ApiOperation("导出Excel")
    public void exportExcel(@RequestBody ExcelExportDTO excelExportDTO, @RequestParam Integer type, HttpServletResponse httpServletResponse) {
        try {
            //获得feign响应流对象
            feign.Response response = gmvTargetService.exportExcel(excelExportDTO, type);
            //如果返回普通json对象，处理异常响应
            if (ExcelExportUtil.validJsonResponse(response.headers().get(ExcelExportUtil.HEAD_CONTENT_TYPE))) {
                ExcelExportUtil.handleProviderErrorResponse(response, httpServletResponse);
            }
            //如果返回excel对象，处理正确响应
            if (ExcelExportUtil.validExcelResponse(response.headers().get(ExcelExportUtil.HEAD_CONTENT_TYPE))) {
                String sourceFileName = "GMV目标";
                ExcelExportUtil.handleProviderRightResponse(response, httpServletResponse, sourceFileName);
            }
        } catch (Exception e) {
            ExcelExportUtil.handleExceptionResponse(httpServletResponse, e);
        }
    }

    @GetMapping("/downLoadTemplate/{templateType}")
    @ApiOperation("下载模板")
    public void downLoadTemplate(@PathVariable("templateType") Integer type, HttpServletResponse response) {
        String fileName = "";
        String sheetName = "";
        if (type.equals(GmvImportTypeEnum.TEMPLATE_DIR_FRA.getType())) {
            try {
                fileName = GmvImportTypeEnum.TEMPLATE_DIR_FRA.getDesc() + "导入模板";
                sheetName = GmvImportTypeEnum.TEMPLATE_DIR_FRA.getDesc();
                ExcelUtil.writeExcel(response, GmvWriteData.getTemplateOneData(), fileName, sheetName, GmvTemplateOne.class);
            } catch (Exception e) {
                throw new RomeException(ResponseMsg.FAIL_2001.getCode(), ResponseMsg.FAIL_2001.getMsg());
            }
        } else if (type.equals(GmvImportTypeEnum.TEMPLATE_APP_DIS_CLOUD.getType())) {
            try {
                fileName = GmvImportTypeEnum.TEMPLATE_APP_DIS_CLOUD.getDesc() + "导入模板";
                sheetName = GmvImportTypeEnum.TEMPLATE_APP_DIS_CLOUD.getDesc();
                ExcelUtil.writeExcel(response, GmvWriteData.getTemplateTwoData(), fileName, sheetName, GmvTemplateTwo.class);
            } catch (Exception e) {
                throw new RomeException(ResponseMsg.FAIL_2001.getCode(), ResponseMsg.FAIL_2001.getMsg());
            }
        } else if (type.equals(GmvImportTypeEnum.ALL_CHANNEL.getType())) {
            try {
                fileName = GmvImportTypeEnum.ALL_CHANNEL.getDesc() + "导入模板";
                sheetName = GmvImportTypeEnum.ALL_CHANNEL.getDesc();
                ExcelUtil.writeExcel(response, GmvWriteData.getTemplateThreeData(), fileName, sheetName, GmvTemplateTwo.class);
            } catch (Exception e) {
                throw new RomeException(ResponseMsg.FAIL_2001.getCode(), ResponseMsg.FAIL_2001.getMsg());
            }
        } else {
            throw new RomeException(ResponseMsg.PARAM_ERROR.getCode(), ResponseMsg.PARAM_ERROR.getMsg());
        }
    }

    @PostMapping("/updateTarget")
    @ApiOperation("更新销售额目标")
    private Response updateTarget(@RequestBody List<GmvTargetDTO> gmvTargetDTO){
        return Response.builderSuccess(gmvTargetService.updateTarget(gmvTargetDTO));
    }

}
